package com.omg.server.util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CommonUtil {
    public static Map<String, String> PROFITLINKS = new HashMap<String, String>();
    private static final Log logger = LogFactory.getLog(CommonUtil.class);
    static{
    	PROFITLINKS.put("s.click.taobao.com", "l=");
    	PROFITLINKS.put("count.chanet.com.cn", "url=");
    	PROFITLINKS.put("p.yiqifa.com", "t=");
    	PROFITLINKS.put("click.union.360buy.com", "to=");
    	PROFITLINKS.put("item.vjia.com", "Source=");
    	PROFITLINKS.put("union.suning.com", "Source=");
    	PROFITLINKS.put("union.suning.com", "vistURL=");
    	PROFITLINKS.put("union.dangdang.com", "backurl=");
    }
    
    public static String getFinalUrl(String targetUrl){
    	String finalUrl = null;
    	if (isNotEmpty(targetUrl)){ 
    		targetUrl = targetUrl.trim();
	    	try {
				URL url = new URL(targetUrl);
				String host = url.getHost();
				//if (PROFITLINKS.containsKey(host)){
					String splitString = PROFITLINKS.get(host);
					int index = -1;
					if (null != splitString){
					  index = targetUrl.indexOf(splitString);
					  if (index > -1){
					     index += splitString.length();
					     finalUrl = targetUrl.substring(index);
					  }
					}
					
					if ("click.union.360buy.com".equals(host)){
						finalUrl = "http://click.union.360buy.com/JdClick/?unionId=26160&t=4&to="+finalUrl;
					}
					else if ("item.vjia.com".equals(host) && index > -1){
						finalUrl = targetUrl.substring(0, index) + "buymefan";
					}
					else if ("detail.tmall.com".equals(host) || host.contains("taobao.com")){
					   int startIndex = targetUrl.indexOf("mm");
					   int endIndex = targetUrl.indexOf("_0_0");
					   if(startIndex > -1 && endIndex > -1 && endIndex > startIndex){
					       String oldValue = targetUrl.substring(startIndex+3, endIndex);
					       finalUrl = targetUrl.replace(oldValue, "30994100");
					   }
					   else if ("s.click.taobao.com".equals(host)){
							String tempUrl = targetUrl;
							while(true){
								if (tempUrl.contains("s.click.taobao.com")){
									tempUrl =  HttpSenderUtil.getTargetUrlByGet(targetUrl, tempUrl);
								}
								else{
									finalUrl = tempUrl;
									break;
								}
							}
						}
					}
					else if ("www.amazon.cn".equals(host) || "www.amazon.com".equals(host)){
						 int beginIndex = targetUrl.indexOf("tag=");
						 if (beginIndex > -1){
							 String subStr = targetUrl.substring(beginIndex+4);
							 int endIndex = subStr.indexOf("&");
							 if (endIndex > -1){
								 subStr = subStr.substring(0, endIndex);
							 }
							 if ("www.amazon.com".equals(host)){
							     targetUrl = targetUrl.replaceAll(subStr, "buymefan-20");
							 }
							 else{
								 targetUrl = targetUrl.replaceAll(subStr, "buymefan-23");
							 }
						 }
					}
					else if ("www.yihaodian.com".equals(host)){
						int beginIndex = targetUrl.indexOf("tracker_u=");
						if (beginIndex > -1){
							 String subStr = targetUrl.substring(beginIndex+10);
							 int endIndex = subStr.indexOf("&");
							 if (endIndex > -1){
								 subStr = subStr.substring(0, endIndex);
							 }
							 targetUrl = targetUrl.replaceAll(subStr, "10256318778");
						}
						else{
							if (targetUrl.indexOf("?") > -1){
								targetUrl = targetUrl + "&tracker_u=10256318778";
							}
							else{
								targetUrl = targetUrl+"?tracker_u=10256318778";
							}
						}
					}
				//}
				
	    	} catch (MalformedURLException e) {
	    		logger.error(e);
			}
    	}
    	
    	if (isEmpty(finalUrl)){
    		finalUrl = targetUrl;
    	}
    	return finalUrl;
    }
    
	public static String getAppPath(){
		String appPath = null;
		if (null != Class.class.getResource("/")){
			appPath = Class.class.getResource("/").getPath()+"/";
		}
		
		if (isEmpty(appPath)){
			appPath = "";
		}
		return appPath;
	}
	
	public static boolean isEmpty(String str){
		return (null == str || "".equals(str.trim()));
	}
	
	public static boolean isNotEmpty(Collection c){
		return (null != c && c.size() > 0);
	}
	
	public static boolean isNotEmpty(Object[] obj){
		return (null != obj && obj.length > 0);
	}
	
	public static boolean isEmpty(Object[] obj){
		return (null == obj || obj.length == 0);
	}
	
	public static boolean isEmpty(Collection c){
		return (null == c || c.size() == 0);
	}
	
	public static boolean isNotEmpty(String str){
		return (null != str && !"".equals(str.trim()));
	}
	
	public static boolean isNumber(String value){
		return isNotEmpty(value) && value.matches("[0-9]+");
	}
	
	public static String createMsgId(String platfromCode){
		StringBuffer buffer = new StringBuffer();
		Date date = new Date();
		buffer.append(DateUtil.formatDate(date, DateUtil.YYYYMMDD));
		buffer.append(platfromCode);
		buffer.append(DateUtil.formatDate(date, DateUtil.HHMMSSSS));
		return buffer.toString();
	}
	
	public static boolean checkIsPrice(char price){
		return String.valueOf(price).matches("(\\.|\\d)");
	}
	
	public static String getPrice(String title){
		String price = null;
		if (CommonUtil.isNotEmpty(title)){
			int index = -1;
			int tmpIndex = -1;
			int len = title.length();
			int lastYuanIndex = -1;
			int yuanIndex = -1;
			boolean isPrice = true;
			if ((index = title.indexOf("满")) > -1 && (yuanIndex=title.indexOf("元")) > -1 && index > yuanIndex){
				tmpIndex = index;
				while (tmpIndex < len){
					if ('元' == title.charAt(tmpIndex)){
						lastYuanIndex = tmpIndex;
					}
					tmpIndex++;
				}
				if(index > -1 && (lastYuanIndex+1) > index)
				price = title.substring(index, lastYuanIndex+1);
			}
			else if ((index = title.indexOf("满")) > -1){
				//tmpIndex = index;
				yuanIndex=title.indexOf("减");
				if (yuanIndex < 0){
					yuanIndex=title.indexOf("返");
				}
				if (yuanIndex > -1){
					yuanIndex++;
					while (yuanIndex < len){
						isPrice = CommonUtil.checkIsPrice(title.charAt(yuanIndex));
						yuanIndex++;
						if (!isPrice){
							break;
						}
					}
					price = title.substring(index, yuanIndex);
				}
			}
			
			if (null == price){
				tmpIndex = title.indexOf("美元");
				boolean isDollar = false;
				if (tmpIndex < 0){
					tmpIndex = title.indexOf("元");
					if (tmpIndex < 0){
						tmpIndex = title.indexOf("$");
						isDollar = true;
						index = tmpIndex;
						if (tmpIndex < 0){//看是否有折扣
							isDollar = false;
							tmpIndex = title.indexOf("折");
							if(tmpIndex > -1){
								index = tmpIndex+1;
							}
							else{
								tmpIndex = title.indexOf("包邮");
								if(tmpIndex > -1){
									 index = tmpIndex;
								}
							}
						}
					}
					else{
					    index = tmpIndex+1;
					}
				}
				else{
					index = tmpIndex+2;
				}
				
				
				while(tmpIndex >= 0){
					if (isDollar){
						tmpIndex = tmpIndex + 1;
						if (tmpIndex > len - 1){
							break;
						}
					}
					else{
					    tmpIndex = tmpIndex - 1;
					    if (tmpIndex < 0){
							break;
						}
					}
					
					isPrice = CommonUtil.checkIsPrice(title.charAt(tmpIndex));
					if (!isPrice){ //非价格信息
						break;
					}
					
				}
				if (index > -1){
					if (isDollar){
						price = title.substring(index, tmpIndex);
					}
					else{
				        price = title.substring(tmpIndex+1, index);
					}
					
				}
			}
		}
		if (null != price){
			price = price.trim();
			if(price.matches("[\\d.]+") && price.indexOf("元") < 0)
			  price += "元";
		}
		return price;
	}
	
	public static void main(String[] args){
		//System.out.println("39d9".matches("[\\d.]+") );
		//System.out.println(getFinalUrl("http://detail.tmall.com/item.htm?id=15883924292&ali_trackid=2:mm_17056287_0_0:1338195288_3z6_826022298"));
		//System.out.println(getFinalUrl("http://www.amazon.com/gp/product/B005PK7ZJO/ref=as_li_ss_tl?ie=UTF8&tag=123hdc-23&linkCode=as2&camp=536&creative=3132&creativeASIN=B005PK7ZJO"));
		System.out.println(getFinalUrl("http://www.amazon.cn/gp/product/B004QM91JE/?tag=zhi-20"));
	}
	
}
